package com.tanhua.server.controller;

import com.tanhua.server.service.PeachBlossomVoiceService;
import com.tanhua.server.vo.AudioVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 桃花传音controller层实现
 */
@Slf4j
@RestController
public class PeachBlossomVoiceController {
    @Autowired
    private PeachBlossomVoiceService peachBlossomVoiceService;

    /**
     * 控制层实现功能:桃花传音之发送语音
     *
     * @param file
     * @return
     */
    @PostMapping("peachblossom")
    public ResponseEntity<Object> peachBlossom(@RequestParam("soundFile") MultipartFile file) {
        try {
            Boolean flag = peachBlossomVoiceService.peachblossomPut(file);
            if (flag) {
                log.info("上传正确!");
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("桃花传音异常!");
            return ResponseEntity.status(500).body(null);
        }
        log.error("桃花传音失败!");
        return ResponseEntity.status(500).body(null);
    }

    /**
     * 控制层实现功能:桃花传音之接收语音
     *
     * @return
     */
    @GetMapping("peachblossom")
    public ResponseEntity<AudioVo> receiveVoice() {
        AudioVo audioVo = null;
        try {
            audioVo = peachBlossomVoiceService.receiveVoice();
        } catch (Exception e) {
            return ResponseEntity.ok(new AudioVo());
        }
        return ResponseEntity.ok(audioVo);
    }


}
